//
// PPAssetsGroupPickerView.m
// PPAssetsPickerViewController
//
// Created by Jinxiao on 9/13/16.
// Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAssetsGroupPickerView.h"

@interface PPAssetsGroupPickerView () <UITableViewDataSource, UITableViewDelegate>
@property (readwrite, nonatomic, strong) UITableView *tableView;

@end


@implementation PPAssetsGroupPickerView

- (instancetype)init {
    self = [super init];

    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollsToTop = NO;
    _tableView.rowHeight = 90;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_tableView];

    UIView *footerView = [[UIView alloc] init];
    footerView.frame = CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN);
    _tableView.tableFooterView = footerView;

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _tableView.frame = self.bounds;
}

- (void)setGroups:(NSArray<ALAssetsGroup *> *)groups {
    _groups = groups;

    [_tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PPAssetsGroupPickerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil) {
        cell = [[PPAssetsGroupPickerViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }

    cell.assetsGroup = [_groups safeObjectAtIndex:indexPath.row];

    return cell;
}


#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_delegate assetsGroupPickerView:self didSelectAssetsGroup:[_groups safeObjectAtIndex:indexPath.row]];
}

@end


@implementation PPAssetsGroupPickerViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    self.textLabel.font = [UIFont systemFontOfSize:17];
    self.detailTextLabel.font = [UIFont systemFontOfSize:11];
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    self.selectedBackgroundView = nil;
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    if(selected) {
        self.accessoryView.hidden = NO;
    } else {
        self.accessoryView.hidden = YES;
    }
}

- (void)setAssetsGroup:(ALAssetsGroup *)assetsGroup {
    _assetsGroup = assetsGroup;

    CGImageRef posterImage = assetsGroup.posterImage;

    self.imageView.image = [UIImage imageWithCGImage:posterImage scale:CGImageGetHeight(posterImage)/80 orientation:UIImageOrientationUp];
    self.textLabel.text = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
    self.detailTextLabel.text = [NSString stringWithFormat:@"%li张", (long)[assetsGroup numberOfAssets]];
}

@end
